/**
 * 仿easyui分页
 */
jQuery.fn.paginationE = function(maxentries, opts){
	opts = jQuery.extend({
		items_per_page:15,
		current_page:1,
		callback:function(){return false;}
	},opts||{});
	return this.each(function() {
		var current_page = opts.current_page;
		var items_per_page = opts.items_per_page;
		var np = numPages();
		/**
		 * 计算最大分页显示数目
		 */
		function numPages() {
			return Math.ceil(maxentries/opts.items_per_page);
		}	
		/**
		 * 分页链接事件处理函数
		 * @参数 {int} page_id 为新页码
		 */
		function pageSelected(page_id,items_per_page,evt){
			current_page = page_id;
			drawLinks();
			var continuePropagation = opts.callback(page_id,items_per_page,panel);
			if (!continuePropagation) {
				if (evt.stopPropagation) {
					evt.stopPropagation();
				}
				else {
					evt.cancelBubble = true;
				}
			}
			return continuePropagation;
		}
		function fun(){//事件绑定
			if(np!=1){
				$(".paginationborder").removeClass("opacityE");
				if($(".paginationNum").val()==np){
					$(".next_").parents(".paginationborder").addClass("opacityE").siblings(".paginationborder").removeClass("opacityE");
				}
				if($(".paginationNum").val()==1){
					$(".prev_").parent().addClass("opacityE").siblings(".paginationborder").removeClass("opacityE");
				}
			}else{
				$(".paginationborder").addClass("opacityE");
			}
			$(".next_").bind('click',function(e){
				if(!$(this).parents(".paginationborder").hasClass("opacityE")){
					pageSelected(np,items_per_page,e);
				}
			});
			$(".prev_").bind('click',function(e){
				if(!$(this).parents(".paginationborder").hasClass("opacityE")){
					pageSelected(1,items_per_page,e);
				}
			});
			$(".prev").bind('click',function(e){
				if(!$(this).parents(".paginationborder").hasClass("opacityE")){
					if (current_page > 1) {
						pageSelected(current_page-1,items_per_page,e);
						return true;
					}
					else {
						return false;
					}
				}
			})
			$(".next").bind('click',function(e){
				if(!$(this).parents(".paginationborder").hasClass("opacityE")){
					if(current_page < numPages()) {
						pageSelected(current_page+1,items_per_page,e);
						return true;
					}
					else {
						return false;
					}
				}
			})
			$(".refresh").bind('click',function(e){
				pageSelected(current_page,items_per_page,e);
				return true;
			})
			$('.paginationNum').bind('keypress',function(e){
				if(event.keyCode == "13"){
					if($('.paginationNum').val()<=np&&$('.paginationNum').val()>0){
						pageSelected($('.paginationNum').val(),items_per_page,e);
						return true;
					}else{
						pageSelected(np,items_per_page,e);
					}
				}
		    });
			$("#pageionSize").on("change",function(e){
				items_per_page=parseInt($("option:selected",this).val());
				pageSelected(1,items_per_page,e);
			});
		}
		function Benum(current_page,items_per_page){
			var Benum =current_page*items_per_page;
			if(Benum<=items_per_page){
				Benum=1;
			}else{
				Benum=(current_page-1)*items_per_page+1;
			}
			return Benum;
		}
		function Ennum(current_page,items_per_page){
			var Ennum =current_page*items_per_page;
			if(np>current_page){
				Ennum=Ennum;
			}else{
				Ennum=maxentries;
			}
			return Ennum;
		}
		/**
		 * 此函数将分页链接插入到容器元素中
		 */
		function drawLinks() {
			panel.empty();
			var lnk =  jQuery(  "<div class='Eleft'>"+
									"<div class='paginationBox'>"+
										"<div>"+
											"<select id='pageionSize'>"+
												"<option "+(items_per_page==15?"selected ='selected'":"")+">15</option>"+
												"<option "+(items_per_page==20?"selected ='selected'":"")+">20</option>"+
												"<option "+(items_per_page==30?"selected ='selected'":"")+">30</option>"+
												"<option "+(items_per_page==50?"selected ='selected'":"")+">50</option>"+
											"</select>"+
										"</div>"+
										"<div class='paginationborder'>"+
											"<a class='prev_ l-btnIcon'></a>"+
											"<span><a class='prev l-btnIcon'></a></span>"+
										"</div>"+
										"<div class='current'>第<input type='text' value="+current_page+" class='paginationNum'>页 共"+np+"页</div>"+
										"<div class='paginationborder'>"+
											"<a class='next l-btnIcon'></a>"+
											"<span><a class='next_ l-btnIcon'></a></span>"+
										"</div>"+
										"<div class='refresh l-btnIcon'></div>"+
									"</div>"+
								"</div>"+
								"<div class='Eright'>当前显示 "+Benum(current_page,items_per_page)+" - "+Ennum(current_page,items_per_page)+" 条记录 共 "+maxentries+" 条记录</div>");
			panel.append(lnk);
			fun();
		}
		var panel = jQuery(this);
		// 所有初始化完成，绘制链接
		drawLinks();
        // 回调函数
        opts.callback(current_page,items_per_page,this);
	});
}


